package com.afkt.project.ui.slice;

import com.afkt.project.base.app.BaseActivity;
import com.ohos.afkt.ResourceTable;
import com.dev.utils.app.TextViewUtils;
import com.dev.utils.app.helper.QuickHelper;
import com.dev.utils.app.toast.ToastTintUtils;
import com.dev.utils.common.ChineseUtils;
import com.dev.utils.common.RandomUtils;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.utils.Color;

public class TextCalcSlice extends BaseActivity {

    private DirectionalLayout vid_atc_linear;


    private DirectionalLayout bar_Layout;
    private DirectionalLayout bar;
    private Image select_retuen;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        bar_Layout = (DirectionalLayout) findComponentById(ResourceTable.Id_base_view_barLayout);
        bar = (DirectionalLayout) LayoutScatter.getInstance(this).parse(ResourceTable.Layout_sheet_dialog_bar, null, false);
        Text text = (Text) bar.findComponentById(ResourceTable.Id_titleText);
        select_retuen = (Image) bar.findComponentById(ResourceTable.Id_sheet_dialog_return);
        text.setText("计算字体高度");
        bar_Layout.addComponent(bar);
        vid_atc_linear = (DirectionalLayout) findComponentById(ResourceTable.Id_vid_atc_linear);
        initValue();
    }

    @Override
    protected int baseLayoutId() {
        return ResourceTable.Layout_activity_text_calc;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initListener() {
        select_retuen.setClickedListener(component -> {
            // LogUtils.error(TAG, "return 点击");
            onBackPressed();
        });
    }

    @Override
    protected void onBackground() {
        super.onBackground();
        bar_Layout.removeAllComponents();
    }

    private void initValue() {
        for (int i = 0; i < 15; i++) {
            String text = ChineseUtils.randomWord(RandomUtils.getRandom(100)) +
                    RandomUtils.getRandomLetters(RandomUtils.getRandom(20));

            String randomText = RandomUtils.getRandom(text.toCharArray(), text.length());

            Text view = QuickHelper.get(new Text(this)).setPadding(30)
                    .setMarginTop(40)
                    .setMarginBottom(20)
                    .setTextColor(Color.BLACK.getValue())
                    .setTextSizeBySp(RandomUtils.getRandom(13, 20))
                    .setBold(RandomUtils.nextBoolean())
                    .setText(randomText).setOnClicks(new Component.ClickedListener() {
                        @Override
                        public void onClick(Component component) {
                            Text textView = (Text) component;
                            String text = textView.getText();
                            StringBuilder builder = new StringBuilder();
                            builder.append("字体总数: ").append(text.length());
                            builder.append("\n字体高度: ").append(TextViewUtils.getTextHeight(textView));
                            builder.append("\n偏移高度: ")
                                    .append(TextViewUtils.getTextTopOffsetHeight(textView));
                            builder.append("\n字体宽度: ").append(TextViewUtils.getTextWidth(textView));
                            builder.append("\n字体大小: ").append(textView.getTextSize());
                            builder.append("\n计算字体大小: ").append(
                                    TextViewUtils.reckonTextSizeByHeight(
                                            TextViewUtils.getTextHeight(textView)
                                    )
                            );
//                            builder.append("\n计算行数: ").append(
//                                    TextViewUtils.calcTextLine(
//                                            textView,
//                                            textView.getWidth()
//                                    )
//                            );
                            String content = builder.toString();
                            ToastTintUtils.normal(content);
                        }
                    }).getView() ;
            view.setMultipleLine(true);
            vid_atc_linear.addComponent(view);
        }
    }
}
